package com.guodu.dao.impl;


import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.SupplierQualifiedDao;
import com.guodu.entity.SupplierQualifiedS;
import com.guodu.entity.SupplierQualified;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.vo.SupplierQualifiedQueryVo;

@Repository("supplierQualifiedDao")
public class SupplierQualifiedDaoImpl  extends BaseDao implements SupplierQualifiedDao{

	@SuppressWarnings("unchecked")
	public List<SupplierQualified> findAll(SupplierQualifiedQueryVo supplierQualifiedQueryVo,Units units,UserInfo userInfo,
			int pageSize, int curPage) {
		StringBuffer hql = new StringBuffer("from SupplierQualified s where 0=0");	
		if(supplierQualifiedQueryVo != null){
			if(supplierQualifiedQueryVo.getYearStart() != null && !supplierQualifiedQueryVo.getYearStart().trim().equals("")){
				hql.append(" and s.supplierYear >="+supplierQualifiedQueryVo.getYearStart());
			}
			if(supplierQualifiedQueryVo.getYearEnd() != null && !supplierQualifiedQueryVo.getYearEnd().trim().equals("")){
				hql.append(" and s.supplierYear <="+supplierQualifiedQueryVo.getYearEnd());
			}
			if(supplierQualifiedQueryVo.getSupply() != null && !supplierQualifiedQueryVo.getSupply().trim().equals("")){
				hql.append(" and s.supply like '%"+supplierQualifiedQueryVo.getSupply()+"%'");
			}
			if(supplierQualifiedQueryVo.getIndustry() != null && !supplierQualifiedQueryVo.getIndustry().trim().equals("")){
				hql.append(" and s.industry like '%"+supplierQualifiedQueryVo.getIndustry()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierName() != null && !supplierQualifiedQueryVo.getSupplierName().trim().equals("")){
				hql.append(" and s.supplierName like '%"+supplierQualifiedQueryVo.getSupplierName()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierCode() != null && !supplierQualifiedQueryVo.getSupplierCode().trim().equals("")){
				hql.append(" and supplierCode like '%"+supplierQualifiedQueryVo.getSupplierCode()+"%'");
			}
			if(supplierQualifiedQueryVo.getCreateTimeStart() != null && !supplierQualifiedQueryVo.getCreateTimeStart().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeStart(supplierQualifiedQueryVo.getCreateTimeStart());
				hql.append(" and s.createTime >= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeStart()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(supplierQualifiedQueryVo.getCreateTimeEnd() != null && !supplierQualifiedQueryVo.getCreateTimeEnd().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeEnd(supplierQualifiedQueryVo.getCreateTimeEnd());
				hql.append(" and s.createTime <= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}		
			if(supplierQualifiedQueryVo.getStatus()!=null&&supplierQualifiedQueryVo.getStatus().trim().length()>0&&!supplierQualifiedQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+supplierQualifiedQueryVo.getStatus());
			}
			if(supplierQualifiedQueryVo.getProducts() != null && supplierQualifiedQueryVo.getProducts().trim().length()>0 ){
				hql.append(" and s.products like '%"+supplierQualifiedQueryVo.getProducts()+"%'");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=s.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}
        hql.append(" order by createTime desc");
		return super.findPages(hql.toString(),curPage,pageSize);
	}
	@SuppressWarnings("unchecked")
	public List<SupplierQualified> downLoad(
			SupplierQualifiedQueryVo supplierQualifiedQueryVo,Units units,UserInfo userInfo) {
		StringBuffer hql = new StringBuffer("from SupplierQualified s where 0=0");	
		if(supplierQualifiedQueryVo != null){
			if(supplierQualifiedQueryVo.getYearStart() != null && !supplierQualifiedQueryVo.getYearStart().trim().equals("")){
				hql.append(" and s.supplierYear >="+supplierQualifiedQueryVo.getYearStart());
			}
			if(supplierQualifiedQueryVo.getYearEnd() != null && !supplierQualifiedQueryVo.getYearEnd().trim().equals("")){
				hql.append(" and s.supplierYear <="+supplierQualifiedQueryVo.getYearEnd());
			}
			if(supplierQualifiedQueryVo.getSupply() != null && !supplierQualifiedQueryVo.getSupply().trim().equals("")){
				hql.append(" and s.supply like '%"+supplierQualifiedQueryVo.getSupply()+"%'");
			}
			if(supplierQualifiedQueryVo.getIndustry() != null && !supplierQualifiedQueryVo.getIndustry().trim().equals("")){
				hql.append(" and s.industry like '%"+supplierQualifiedQueryVo.getIndustry()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierName() != null && !supplierQualifiedQueryVo.getSupplierName().trim().equals("")){
				hql.append(" and s.supplierName like '%"+supplierQualifiedQueryVo.getSupplierName()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierCode() != null && !supplierQualifiedQueryVo.getSupplierCode().trim().equals("")){
				hql.append(" and supplierCode like '%"+supplierQualifiedQueryVo.getSupplierCode()+"%'");
			}
			if(supplierQualifiedQueryVo.getCreateTimeStart() != null && !supplierQualifiedQueryVo.getCreateTimeStart().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeStart(supplierQualifiedQueryVo.getCreateTimeStart());
				hql.append(" and s.createTime >= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeStart()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(supplierQualifiedQueryVo.getCreateTimeEnd() != null && !supplierQualifiedQueryVo.getCreateTimeEnd().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeEnd(supplierQualifiedQueryVo.getCreateTimeEnd());
				hql.append(" and s.createTime <= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}		
			if(supplierQualifiedQueryVo.getStatus()!=null&&supplierQualifiedQueryVo.getStatus().trim().length()>0&&!supplierQualifiedQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+supplierQualifiedQueryVo.getStatus());
			}
			if(supplierQualifiedQueryVo.getProducts() != null && supplierQualifiedQueryVo.getProducts().trim().length()>0 ){
				hql.append(" and s.products like '%"+supplierQualifiedQueryVo.getProducts()+"%'");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=s.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
	}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
		hql.append(" and createPeople = "+userInfo.getUserId());
	}
		
		hql.append(" order by createTime desc");
		return super.find(hql.toString());
	}

	
	public void examinAll(SupplierQualifiedQueryVo supplierQualifiedQueryVo,
			Units units, UserInfo userInfo) {
		StringBuffer hql = new StringBuffer();
		if(units == null){
			hql.append("update from SupplierQualified s set s.status=3 where s.status=0 ");
		}else if(units.getUnitsLevel() == 1L && units.getIsComplete()==1L){
			hql.append("update from SupplierQualified s set s.status=1 where s.status=0 ");
		}
		if(supplierQualifiedQueryVo != null){
			if(supplierQualifiedQueryVo.getYearStart() != null && !supplierQualifiedQueryVo.getYearStart().trim().equals("")){
				hql.append(" and s.supplierYear >="+supplierQualifiedQueryVo.getYearStart());
			}
			if(supplierQualifiedQueryVo.getYearEnd() != null && !supplierQualifiedQueryVo.getYearEnd().trim().equals("")){
				hql.append(" and s.supplierYear <="+supplierQualifiedQueryVo.getYearEnd());
			}
			if(supplierQualifiedQueryVo.getSupply() != null && !supplierQualifiedQueryVo.getSupply().trim().equals("")){
				hql.append(" and s.supply like '%"+supplierQualifiedQueryVo.getSupply()+"%'");
			}
			if(supplierQualifiedQueryVo.getIndustry() != null && !supplierQualifiedQueryVo.getIndustry().trim().equals("")){
				hql.append(" and s.industry like '%"+supplierQualifiedQueryVo.getIndustry()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierName() != null && !supplierQualifiedQueryVo.getSupplierName().trim().equals("")){
				hql.append(" and s.supplierName like '%"+supplierQualifiedQueryVo.getSupplierName()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierCode() != null && !supplierQualifiedQueryVo.getSupplierCode().trim().equals("")){
				hql.append(" and supplierCode like '%"+supplierQualifiedQueryVo.getSupplierCode()+"%'");
			}
			if(supplierQualifiedQueryVo.getCreateTimeStart() != null && !supplierQualifiedQueryVo.getCreateTimeStart().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeStart(supplierQualifiedQueryVo.getCreateTimeStart());
				hql.append(" and s.createTime >= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeStart()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(supplierQualifiedQueryVo.getCreateTimeEnd() != null && !supplierQualifiedQueryVo.getCreateTimeEnd().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeEnd(supplierQualifiedQueryVo.getCreateTimeEnd());
				hql.append(" and s.createTime <= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}		
			if(supplierQualifiedQueryVo.getStatus()!=null&&supplierQualifiedQueryVo.getStatus().trim().length()>0&&!supplierQualifiedQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+supplierQualifiedQueryVo.getStatus());
			}
			if(supplierQualifiedQueryVo.getProducts() != null && supplierQualifiedQueryVo.getProducts().trim().length()>0 ){
				hql.append(" and s.products like '%"+supplierQualifiedQueryVo.getProducts()+"%'");
			}
		}
		if(units==null){
		hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=s.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
	    }else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
		hql.append(" and createPeople = "+userInfo.getUserId());
	    }	
		super.executeUpdate(hql.toString());
	}
	public int findCount(SupplierQualifiedQueryVo supplierQualifiedQueryVo,Units units,UserInfo userInfo) {
		StringBuffer hql = new StringBuffer("select count(*) from SupplierQualified s where 0=0");
		if(supplierQualifiedQueryVo != null){
			if(supplierQualifiedQueryVo.getYearStart() != null && !supplierQualifiedQueryVo.getYearStart().trim().equals("")){
				hql.append(" and s.supplierYear >="+supplierQualifiedQueryVo.getYearStart());
			}
			if(supplierQualifiedQueryVo.getYearEnd() != null && !supplierQualifiedQueryVo.getYearEnd().trim().equals("")){
				hql.append(" and s.supplierYear <="+supplierQualifiedQueryVo.getYearEnd());
			}
			if(supplierQualifiedQueryVo.getSupply() != null && !supplierQualifiedQueryVo.getSupply().trim().equals("")){
				hql.append(" and s.supply like '%"+supplierQualifiedQueryVo.getSupply()+"%'");
			}
			if(supplierQualifiedQueryVo.getIndustry() != null && !supplierQualifiedQueryVo.getIndustry().trim().equals("")){
				hql.append(" and s.industry like '%"+supplierQualifiedQueryVo.getIndustry()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierName() != null && !supplierQualifiedQueryVo.getSupplierName().trim().equals("")){
				hql.append(" and s.supplierName like '%"+supplierQualifiedQueryVo.getSupplierName()+"%'");
			}
			if(supplierQualifiedQueryVo.getSupplierCode() != null && !supplierQualifiedQueryVo.getSupplierCode().trim().equals("")){
				hql.append(" and supplierCode like '%"+supplierQualifiedQueryVo.getSupplierCode()+"%'");
			}
			if(supplierQualifiedQueryVo.getCreateTimeStart() != null && !supplierQualifiedQueryVo.getCreateTimeStart().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeStart(supplierQualifiedQueryVo.getCreateTimeStart());
				hql.append(" and s.createTime >= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeStart()+"  00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(supplierQualifiedQueryVo.getCreateTimeEnd() != null && !supplierQualifiedQueryVo.getCreateTimeEnd().trim().equals("")){
				supplierQualifiedQueryVo.setCreateTimeEnd(supplierQualifiedQueryVo.getCreateTimeEnd());
				hql.append(" and s.createTime <= TO_DATE('"+supplierQualifiedQueryVo.getCreateTimeEnd()+"  23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(supplierQualifiedQueryVo.getStatus()!=null&&supplierQualifiedQueryVo.getStatus().trim().length()>0&&!supplierQualifiedQueryVo.getStatus().equals("10")){
				hql.append(" and status ="+supplierQualifiedQueryVo.getStatus());
			}
			if(supplierQualifiedQueryVo.getProducts() != null && supplierQualifiedQueryVo.getProducts().trim().length()>0 ){
				hql.append(" and s.products like '%"+supplierQualifiedQueryVo.getProducts()+"%'");
			}
			
		}
		if(units==null){
			hql.append(" and (status=1 or (exists(select 1 from UserInfo u where u.userId=s.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1L){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}
		return super.findInt(hql.toString());
	}

	
	public void save(SupplierQualified supplierQualified) {
				
		super.save(supplierQualified);
		
	}

	
	public void examine(String ids, Long status) {
		StringBuffer hql = new StringBuffer("update SupplierQualified set status =");
		hql.append(status);
		hql.append(" where supplierId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}
	

	public void changeStatus(List<SupplierQualifiedS> entities) {
		super.saveAll(entities);
		
	}

	
	public void delByIds(String[] ids) {
		super.deleteAllById(SupplierQualified.class,ids);
		
	}
	
    public SupplierQualified findById(String id){
		
		return (SupplierQualified) super.findById(SupplierQualified.class, id);
	}

	public void update(SupplierQualified supplierQualified) {
		 super.update(supplierQualified);
		
	}
	@SuppressWarnings("unchecked")
	public List<SupplierQualifiedS> findStatus(String id) {	
		
		return super.findByProperty(SupplierQualifiedS.class,"supplierId",id);
	}

	public void deleteById(String id) {
		
		super.delete(SupplierQualified.class, id);
	}
	
	

	
	

	

	
	
	
	
	
	
}
